/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package achi.community.irla.database;

import achi.community.irla.beans.CompletedBackBean;
import achi.community.irla.entities.Achievements_completed;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Enumeration;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author hajo
 */
@WebServlet(name = "TestServlet", urlPatterns = {"/testar"})
public class TestServlet extends HttpServlet implements Serializable {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("HEJ!!");

        response.setContentType("text/html;charset=UTF-8");


        //fillCompleteBean(cAchiKey);
        //CompletedBackBean compBean = (CompletedBackBean)request.getSession().getAttribute("completedBackBean");
        //response.sendRedirect("completed.jsf" );
        //getServletContext().getRequestDispatcher().forward("second page");

        String cAchiId = request.getParameter("completedAchi");
        
//         CompletedBackBean completedBackBean = (CompletedBackBean) request.getAttribute("completedBackBean");
        Enumeration<String> a = request.getAttributeNames();
        
        while(a.hasMoreElements()){
            System.out.println("atrib:"+a.nextElement());
        }
         //ModelBean modelBean = (ModelBean) request.getAttribute("modelBean");
         
         //System.out.println(completedBackBean);
         //completedBackBean.setDescription("aaaaaaaaaaaaa");
       // Achievements_completed cAchi = modelBean.getAchiCompleted(cAchiId);
        //completedBackBean.setUser(modelBean.getUser());
        //CompletedBackBean compBackBean = fillCompleteBean(request, cAchiKey);
        response.sendRedirect("completed.jsf");
        
       // if (cAchiKey != null) {
            // Achievements_completed cAchi = modelBean.getAchiCompleted(cAchiKey);
            //completedBackBean.setDescription("aaa");
        //}

        /**
         * System.out.println("aa1"); FacesContext facesContext =
         * FacesContext.getCurrentInstance(); System.out.println("aa2");
         * ExternalContext ec = facesContext.getExternalContext();
         * System.out.println("HHHEEEEJJ!!!"); ec.redirect("completed.jsp");
         *
         * //String action = request.getParameter("action");
         * System.out.println("HHHEEEEJJ2!!!"); // if ("book".equals(action)) {
         * //} //message(response.getWriter());
        *
         */
    }

    private void message(PrintWriter out) {
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet TestServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Done</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    public CompletedBackBean fillCompleteBean(HttpServletRequest request, String cAchiId) {
        CompletedBackBean completedBackBean = (CompletedBackBean) request.getSession().getAttribute("completedBackBean");
        ModelBean modelBean = (ModelBean) request.getSession().getAttribute("modelBean");
        Achievements_completed cAchi = modelBean.getAchiCompleted(cAchiId);
        completedBackBean.setUser(cAchi.getUser());
        completedBackBean.setDate_completed(cAchi.getDate_completed());
        completedBackBean.setDescription(cAchi.getDescription());
        completedBackBean.setAchievement(cAchi.getAchievement());
        completedBackBean.setImages(cAchi.getImages());
        return completedBackBean;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
